package com.kavin.rgb;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 图片颜色反转
 */
public class BitmapUtils {
    public static void reversalColor(ImageView image) {
        //image.setDrawingCacheEnabled(true);
        //Bitmap bitmap = image.getDrawingCache();
        //需要注意的是，再取出Cache之后，一定要调用,否则图片的视图永远是原来的样子。
        //image.setDrawingCacheEnabled(false);
        Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
        Bitmap bitmap1 = reversalColor(bitmap);
        image.setImageBitmap(bitmap1);
    }

    /**
     * 反转颜色
     */
    private static Bitmap reversalColor(Bitmap bitmap) {
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        int[][] colorArr = new int[bitmap.getWidth()][bitmap.getHeight()];
        for (int i = 0; i < bitmap.getWidth(); i++) {
            for (int j = 0; j < bitmap.getHeight(); j++) {
                colorArr[i][j] = bitmap.getPixel(i, j);

                int pixel = bitmap.getPixel(i, j);
                int color = reversalColor(pixel);
                bitmap2.setPixel(i, j, color);
            }
        }

        colorArr = null;
        bitmap.recycle();

        return bitmap2;
    }

    public static void greyColor(ImageView image) {
        Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
        Bitmap bitmap1 = greyColor(bitmap);
        image.setImageBitmap(bitmap1);
    }

    /**
     * 图片灰度处理
     */
    private static Bitmap greyColor(Bitmap bitmap) {
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        int[][] colorArr = new int[bitmap.getWidth()][bitmap.getHeight()];
        for (int i = 0; i < bitmap.getWidth(); i++) {
            for (int j = 0; j < bitmap.getHeight(); j++) {
                colorArr[i][j] = bitmap.getPixel(i, j);

                int pixel = bitmap.getPixel(i, j);
                int color = greyColor(pixel);
                bitmap2.setPixel(i, j, color);
            }
        }

        colorArr = null;
        bitmap.recycle();

        return bitmap2;
    }


    /**
     * 将颜色从int 拆分成argb,取反
     */
    public static int reversalColor(int color) {
        int a = Color.alpha(color);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        //Log.d(msg, "----a:" + a + ", r:" + r + ", g:" + g + ", b:" + b);
        //颜色取反
        int argb = Color.argb(a, 255 - r, 255 - g, 255 - b);

        return argb;
    }

    /**
     * 配凑灰颜色
     */
    public static int greyColor(int color) {
        int a = Color.alpha(color);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        //拼凑出新的颜色
        int grey = (int) (r * 0.3 + g * 0.59 + b * 0.11);
        return Color.argb(a, grey, grey, grey);
    }


    /**
     * 保存bitmap到本地
     *
     * @param path    路径
     * @param bitmap 图片
     * @return 路径
     */
    public static boolean saveBitmap(String path, Bitmap bitmap) {
        //File path = new File(path + File.separator + "a.png");
        try {
            FileOutputStream fos = new FileOutputStream(path);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;


    }
}
